<?php

namespace App\Http\Controllers\Web;

use App\Exceptions\MsgException;
use App\Http\Controllers\Controller;
use App\Http\Requests\OrderStoreRequest;
use App\Models\Banner;
use App\Models\Order;
use App\Models\Product;
use App\Repositories\OrderRepository;
use App\Services\CartService;
use App\Services\OrderService;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;
use \Illuminate\Support\Facades\Validator;
class OrderController extends BaseController
{
    /**
     * @var OrderRepository
     */
    private $orderRepository;

    /**
     * OrderController constructor.
     * @param OrderRepository $orderRepository
     */
    public function __construct(OrderRepository $orderRepository)
    {
        $this->orderRepository = $orderRepository;
    }

    /**
     * 订单查询
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function check(Request $request){
        if($request->email && $request->phone){

            if (!captcha_check($request->captcha_code)) {
                return $this->error('查詢失敗','驗證碼錯誤','/check');
            }

            $order = $this->orderRepository->getByPhoneEmail($request->phone,$request->email);
            if($order){
                return template('checking',compact('order'));
            }else{
                return $this->error('查詢失敗','該訂單數據不存在','/check');
            }
        }
        $banner = Banner::where('page','/check')->where('type',0)->first();
        return template('check')->with('banner',$banner);
    }

    /**
     * 订单下单成
     * @param $no
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function checking($no){

        $order = $this->orderRepository->getByNo($no);
        if(!$order){
            abort(404);
        }
        return template('checking',compact('order'));
    }

    /**
     * 订单结算页
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function shopping(Request $request){
        $cart = CartService::getCart();
        //token 防止多次提交
        $form_token = md5(time());
        //将token存入session
        $request->session()->put('form_token',$form_token);
        return template('shopping',compact('cart','form_token'));
    }


    /**
     * 订单提交
     * @param OrderStoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(OrderStoreRequest $request){

        try {
            $form_token = $request->input('form_token');
            if( !$request->session()->get('form_token') || $request->session()->get('form_token')!=$form_token ){
                //throw new MsgException('請勿重複提交！');
            }
            $request->session()->put('form_token',null);
            $order = $this->orderRepository->store($request->all());
            Cookie::queue(Cookie::forget('carts'));
            session()->flash('flash',json_encode(['type'=>'tips','msg'=>'訂單提交成功','sub_msg'=>'我們將會儘快為您安排發貨','code'=>200,'form_data'=>request()->except('_token','_method')],JSON_UNESCAPED_UNICODE));
            return response()->json(['code'=>200,'msg'=>'訂單提交成功','data'=>['id'=>$order->no]]);
        }catch (MsgException $exception){
            return response()->json(['code'=>400,'msg'=>$exception->getMessage()],400);
        }catch (QueryException $exception){
            return response()->json(['code'=>400,'msg'=>'系統出現異常！'],400);
        }catch (\Exception $exception){
            return response()->json(['code'=>400,'msg'=>'系統出現異常！'],400);
        }

    }
}
